package com.xiaocikuai.smallchunks.service.Transaction.impl;

import com.xiaocikuai.smallchunks.entity.Transaction.MaleList;
import com.xiaocikuai.smallchunks.entity.Transaction.Order;
import com.xiaocikuai.smallchunks.entity.Transaction.OrderVO;
import com.xiaocikuai.smallchunks.entity.Transaction.ReturnOrderList;
import com.xiaocikuai.smallchunks.mapper.Transaction.MaleListMapper;
import com.xiaocikuai.smallchunks.mapper.Transaction.OrderMapper;
import com.xiaocikuai.smallchunks.service.Transaction.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaocikuai.smallchunks.utils.JWTservice.JwtService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author1
 * @since 2024-07-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Resource
    private JwtService jwtService;
    @Resource
    private MaleListMapper maleListMapper;

    public List<OrderVO> selectUserOrder(Order order, String token) {
        List<OrderVO> orders = new ArrayList<>();
        switch (order.getMode()) {
            case 0 -> orders = selectUserOrderInfo(token);
            case 1 -> orders = selectUserOrderAP(token); // 待付款
            case 2 -> orders = selectUserOrderAR(token); // 待收货
            case 3 -> orders = selectUserOrderF(token); // 已完成
        }
        return orders;
    }

    public List<OrderVO> selectUserOrderInfo(String token) {
        int uid = jwtService.deconstruction(token).getUid();
        return getOderInfo(lambdaQuery().eq(Order::getUid, uid)
                .orderByDesc(Order::getCreateTime) // 按照创建时间降序排序（最近在前）
                .select().list());
    }

    public List<OrderVO> selectUserOrderAP(String token) {
        int uid = jwtService.deconstruction(token).getUid();
        return getOderInfo(lambdaQuery().eq(Order::getUid, uid)
                .eq(Order::getStatus, 0)
                .orderByDesc(Order::getCreateTime) // 按照创建时间降序排序（最近在前）
                .select().list());
    }

    public List<OrderVO> selectUserOrderAR(String token) {
        int uid = jwtService.deconstruction(token).getUid();
        return getOderInfo(lambdaQuery().eq(Order::getUid, uid)
                .eq(Order::getStatus, 2)
                .orderByDesc(Order::getCreateTime) // 按照创建时间降序排序（最近在前）
                .select().list());
    }

    public List<OrderVO> selectUserOrderF(String token) {
        int uid = jwtService.deconstruction(token).getUid();
        return getOderInfo(lambdaQuery().eq(Order::getUid, uid)
                .eq(Order::getStatus, 1)
                .orderByDesc(Order::getCreateTime) // 按照创建时间降序排序（最近在前）
                .select().list());
    }

    public List<OrderVO> getOderInfo(List<Order> orderList){
        List<OrderVO> returnOrderLists = new ArrayList<> ();
        for (Order order: orderList){
            OrderVO returns = new OrderVO();
            MaleList male = maleListMapper.selectById(order.getTid());
            BeanUtils.copyProperties(order,returns);
            BeanUtils.copyProperties(male,returns);
            returnOrderLists.add(returns);
        }
        return returnOrderLists;
    }
}
